﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class PunchInDialog : ChildWindow
    {
        public PunchInDialog()
        {
            InitializeComponent();
            // Load the role combo-box
            foreach (ServiceReferenceOpenRPOS.Role role in Globals.db.RoleRecords)
                foreach (ServiceReferenceOpenRPOS.EmployeeRole employeeRole in Globals.db.EmployeeRoleRecords)
            {
                if (Globals.currentEmployee.col_ID == employeeRole.col_EmployeeID && employeeRole.col_RoleID == role.col_ID)
                    cmbRole.Items.Add(role.col_Title);
            }
            if (cmbRole.Items.Count() > 0)
                cmbRole.SelectedIndex = 0;
        }

        private async void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ServiceReferenceOpenRPOS.TimeSheet timeSheet = new ServiceReferenceOpenRPOS.TimeSheet();
            timeSheet.bFilterPunchType = true;
            timeSheet.col_PunchType = true; // In-punch
            timeSheet.bFilterEmployeeID = true;
            timeSheet.col_EmployeeID = Globals.currentEmployee.col_ID;
            timeSheet.bFilterPunchDateTime = true;
            timeSheet.col_PunchDateTime = DateTime.Now;
            timeSheet.bFilterRoleID = true;
            timeSheet.col_RoleID = Globals.db.RoleRecords.Where(x => x.col_Title == (string)cmbRole.SelectedValue).First().col_ID;
            await Globals.db.AddTimeSheetObject(timeSheet);
            // Re-initialize the time sheet data
            Globals.timeSheet.dpPeriodStart.SelectedDate = timeSheet.col_PunchDateTime;
            Globals.mainPage.ContentFrame.Content = Globals.timeSheet;
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

